package com.solarroast.mirrorarray.components;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import com.solarroast.mirrorarray.beans.MAIBean;

public class MirrorArray {
	private ArrayList<ArrayList<MirrorArrayItem>> mArray;
	private TreeMap<String, MAIBean> selectedMAIs;
	
	public MirrorArray(int numColumns, int numRows, int numMirrors) {
		mArray = new ArrayList<ArrayList<MirrorArrayItem>>();
		selectedMAIs = new TreeMap<String, MAIBean>();
		
		for(int r = 0; r < numRows; r++) {
			ArrayList<MirrorArrayItem> mArrayRow = new ArrayList<MirrorArrayItem>();
			
			for(int c = 0; c < numColumns; c++) {
				MirrorArrayItem mArrayItem = new MirrorArrayItem(this, numMirrors, r, c);
				mArrayRow.add(mArrayItem);
			}
			
			mArray.add(mArrayRow);
		}
	}
	
	public MirrorArrayItem getMirrorArrayItem(int row, int col) {
		return mArray.get(row).get(col);
	}
	
	public int getNumMirrors() {
		int numMirrors = 0;
		
		for(int r = 0; r < mArray.size(); r++) {
			for(int c = 0; c < mArray.get(r).size(); c++) {
				numMirrors += mArray.get(r).get(c).getNumMirrors();	
			}
		}
		
		return numMirrors;
	}
	
	public void setAllMirrors(boolean state) {
		if(state) {
			for(int r = 0; r < mArray.size(); r++) {
				for(int c = 0; c < mArray.get(r).size(); c++) {
					if(!mArray.get(r).get(c).isActive()) {
						mArray.get(r).get(c).toggleActive();
						selectedMAIs.put(mArray.get(r).get(c).getBean().getName(), mArray.get(r).get(c).getBean());
					}
				}
			}
		}
		else {
			selectedMAIs.clear();
			
			for(int r = 0; r < mArray.size(); r++) {
				for(int c = 0; c < mArray.get(r).size(); c++) {
					if(mArray.get(r).get(c).isActive())
						mArray.get(r).get(c).toggleActive();
				}
			}
		}
	}
	
	public boolean hasActiveMAIs() {
		if(selectedMAIs.size() > 0)
			return true;
		
		return false;
	}
	
	public int getNumColumns() {
		return mArray.size();
	}
	
	public void printActiveMAIs() {
		for(int r = 0; r < mArray.size(); r++) {
			for(int c = 0; c < mArray.get(r).size(); c++) {
				if(mArray.get(r).get(c).isActive())
					System.out.println(r + "-" + c + ": ACTIVE");
			}
		}
	}
	
	public ArrayList<MAIBean> getSelection() {
		MAIBean currBean;
		ArrayList<MAIBean> selection = new ArrayList<MAIBean>();
		
		for(Map.Entry<String, MAIBean> entry : selectedMAIs.entrySet()) {
			currBean = entry.getValue();
			
			selection.add(currBean);
		}
		
		return selection;
	}
	
	public void setActiveMAIs(boolean activeState) {
		MAIBean currBean;
		
		System.out.println("Toggling active state on: ");
		
		for(Map.Entry<String, MAIBean> entry : selectedMAIs.entrySet()) {
			currBean = entry.getValue();
			
			mArray.get(currBean.getRow()).get(currBean.getColumn()).toggleMirrorsActive(activeState);
			
			System.out.println(currBean.getName());
		}
	}
	
	public void addToMap(MAIBean MAIBeanItem) {
		selectedMAIs.put(MAIBeanItem.getName(), MAIBeanItem);
	}
	
	public void removeFromMap(MAIBean MAIBeanItem) {
		selectedMAIs.remove(MAIBeanItem.getName());
	}
	
	public void clearMap() {		
		ArrayList<MAIBean> MAIBeans = new ArrayList<MAIBean>();
		
		// build our list of beans to clean
		for(Map.Entry<String, MAIBean> entry : selectedMAIs.entrySet()) {
			MAIBeans.add(entry.getValue());
		}
		
		// clean and clear
		for(int b = 0; b < MAIBeans.size(); b++) {
			mArray.get(MAIBeans.get(b).getRow()).get(MAIBeans.get(b).getColumn()).toggleActive();
		}
		
		selectedMAIs.clear();
	}
}
